package org.jeecg.modules.demo.hydrology.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.hydrology.dto.DbcxcddyDto;
import org.jeecg.modules.demo.hydrology.dto.DbcxcdssDto;
import org.jeecg.modules.demo.hydrology.entity.Dbcxcddy;
import org.jeecg.modules.demo.hydrology.entity.Dbcxcdss;
import org.jeecg.modules.demo.hydrology.mapper.DbcxcddyMapper;
import org.jeecg.modules.demo.hydrology.mapper.DbcxcdssMapper;
import org.jeecg.modules.demo.hydrology.service.IDbcxcdssService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 地表沉陷实时
 * @Author: jeecg-boot
 * @Date:   2024-04-17
 * @Version: V1.0
 */
@Service
public class DbcxcdssServiceImpl extends ServiceImpl<DbcxcdssMapper, Dbcxcdss> implements IDbcxcdssService {
    @Autowired
    private DbcxcdssMapper dbcxcdssMapper;
    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private SysDictItemMapper sysDictItemMapper;

    /**
     * 地表沉陷实时-分页列表查询
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public IPage<DbcxcdssDto> getList(Page<Dbcxcdss> page, QueryWrapper<Dbcxcdss> queryWrapper) {
        Page<Dbcxcdss> dbcxcdssPage = dbcxcdssMapper.selectPage(page, queryWrapper);
        List<DbcxcdssDto> collect = dbcxcdssPage.getRecords().stream().map(dbcxcdss -> {
            DbcxcdssDto dbcxcdssDto = new DbcxcdssDto();
            BeanUtils.copyProperties(dbcxcdss, dbcxcdssDto);
            QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();
            sysDepartQueryWrapper.eq("org_code", dbcxcdss.getStationMine());
            SysDepart sysDepart = sysDepartMapper.selectOne(sysDepartQueryWrapper);
            if (sysDepart != null){
                dbcxcdssDto.setStationName(sysDepart.getDepartName());
            }
            QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<>();
            sysDictQueryWrapper.eq("dict_code","point_status");
            SysDict sysDict = sysDictMapper.selectOne(sysDictQueryWrapper);
            if (sysDict != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
                sysDictItemQueryWrapper.eq("dict_id",sysDict.getId()).eq("item_value",dbcxcdss.getStationStatus());
                SysDictItem sysDictItem = sysDictItemMapper.selectOne(sysDictItemQueryWrapper);
                if (sysDictItem != null) {
                    dbcxcdssDto.setStationStatusItemText(sysDictItem.getItemText());
                }
            }
            return dbcxcdssDto;
        }).collect(Collectors.toList());
        IPage<DbcxcdssDto> dbcxcdssDtoIPage = new Page<>();
        dbcxcdssDtoIPage.setRecords(collect);
        dbcxcdssDtoIPage.setTotal(dbcxcdssPage.getTotal());
        dbcxcdssDtoIPage.setPages(page.getPages());
        dbcxcdssDtoIPage.setSize(page.getSize());
        return dbcxcdssDtoIPage;
    }
}
